﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace SSO1.Controllers
{
    public class UserController : Controller
    {
        public IActionResult Index()
        {
            string userName = HttpContext.Session.GetString("user");

            if (string.IsNullOrWhiteSpace(userName))
            {
                return RedirectToAction("Login");
            }
            else
            {
                ViewBag.UserName = userName;
                ViewBag.LoginTime = HttpContext.Session.GetString("loginTime");
                ViewBag.SessionID = HttpContext.Session.Id;
            }

            return View();

        }


        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }



        [HttpPost]
        public IActionResult Login(string userName, string pwd)
        {
            if (userName == "admin" && pwd == "123")
            {
                HttpContext.Session.SetString("user", "admin");
                HttpContext.Session.SetString("loginTime", DateTime.Now.ToString());

                return RedirectToAction("Index");
            }

            return View();
        }


        public IActionResult LogOut()
        {
            HttpContext.Session.Remove("user");
            HttpContext.Session.Remove("loginTime");

            return RedirectToAction("Login");
        }

    }
}
